package top.saplf.sileny.reader.shelf

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.jakewharton.rxbinding2.view.RxView
import kotlinx.android.synthetic.main.item_book_mark.view.*
import top.saplf.sileny.R

/**
 * @author saplf
 */
class BookMarkAdapter : BaseQuickAdapter<BookMark, BaseViewHolder>(R.layout.item_book_mark) {
    var deleteAction: ((BookMark) -> Boolean)? = null
    var itemAction: ((BookMark) -> Unit)? = null

    override fun convert(helper: BaseViewHolder, item: BookMark) {
        with(helper.itemView) {
            preview.text = item.snapshot
            RxView.clicks(preview)
                    .subscribe {
                        itemAction?.invoke(item)
                    }
            RxView.clicks(delete)
                    .subscribe {
                        if (deleteAction?.invoke(item) ?: false) {
                            remove(helper.adapterPosition)
                        }
                    }
        }
    }
}